<?php
/**
 * Created by PhpStorm.
 * User: silov
 * Date: 16/3/29
 * Time: 17:32
 */
namespace Server\Controller;

use Think\Controller;
use Think\Upload;

class FileController extends Controller
{
    public function upload()
    {
        $upload = new Upload();
        $upload->maxSize = 1024*1024*2;
        $upload->exts = ['jpg', 'jpeg', 'gif', 'png'];
        $upload->rootPath = ROOT_PATH . '/Static/upload/img/' ;

        $info = $upload->upload($_FILES);
        $fieldName = empty($this->params['key']) ? 'file' : $this->params['key'];
        //$filePath =  getNewPicsName($fileInfo['fileName'],640);
        $fileInfo =  $info[$fieldName];
        if (!$fileInfo) {
            $this->ajaxReturn(array('status' => 0, 'msg' => $upload->getError()));
            exit;
        } else {
            $data = array(
                'url'       => STATIC_URL . '/upload/img/'. $fileInfo['savepath'] . $fileInfo['savename'],
                'title'     => $fileInfo['name'],
                'original'  => $fileInfo['name'],
                'type'      => $fileInfo['type'],
                'size'      => $fileInfo['size']
            );
        }
        $this->jsonReturn(200, $data);
    }
}